#!/usr/bin/python

import os, re
from cubaCodeTemplate import *
from string import zfill

listdir = os.listdir('.')

### Envs (TODO: via settings.py)
localPath = os.getcwd() 
cubaPath  = os.path.expanduser('~')+"/cuba/Cuba-3.0"

env = Environment(  LIBS=['m','cuba'], 
                    LIBPATH=cubaPath, CPPPATH=[cubaPath],
                    CCFLAGS='-O2',
                    platform='posix',ENV=os.environ) 


pat = re.compile('.*func.*c$')
## list of func-files
obj_src = sorted([i for i in listdir if pat.match(i)])
## list of *.o-files:
objs = [ s[:-1]+'o' for s in obj_src]
## list of headers:
#headers   = [ os.path.join(localPath,s[:-1]+'h') for s in obj_src]
num = len(obj_src) # <-- for zfill() and test purposes


### Loop over all func-files.
### Every iteration produces one final binary cuba-file.
for src in obj_src:
## compile object files
    Object(src)

    i = int(src.split("_func_")[1].split("_")[0])
    obj =  src[:-1]+'o'
    #print "fileName = %s, i = %s"%(src,i)
    function = "f = func_t_%d(xx);" % i
    header   = os.path.join(localPath,src[:-1]+'h')
    include = "#include \"%s\"\n"%(header)

    cubaFile = os.path.join(localPath,localPath.split('/')[-1]+"_cuba__%s.c" \
        %zfill(i,len(str(num))))  
    f = open(cubaFile,'w')
    f.write(coreCubaCodeTemplate.format(
            includes = include,
            cuba_path= os.path.join(cubaPath,'cuba.h'),
            functions= function))
    f.close()
    print "Compiling", cubaFile
    targetName = "cuba__%s.run" %zfill(i,len(str(num)))
    env.Program(target = targetName, source = [cubaFile]+[obj])

## Do nothing, just points out what to clean with '-c' option (see man scons)
env.Clean("cleanObjs",objs)
